package controllers.caterinaController.controllers;


import controllers.Checks;
import controllers.ExceptionsTypes;
import controllers.Permission;
import controllers.PermissionList;
import controllers.dbWrapper.DBWrapper;
import controllers.dbWrapper.UserWrapper;

import person.*;

public class LoginManager {

	public static User login(String userName, String password) throws Exception{
		Checks.checkUser(userName, password,false);
		currentLogged = UserWrapper.getUser(userName, password, true);
		if(currentLogged == null)
			throw new Exception(ExceptionsTypes.LOGIN_ERROR.name());
		return currentLogged;
	}
	
	public static User getCurrentLogged() throws Exception{
		if(currentLogged == null)
			throw new Exception(ExceptionsTypes.INVALID_TOKEN.name());
		if(DBWrapper.getConnection() == null)
			throw new Exception(ExceptionsTypes.CONNECTION_ERROR.name());
		return currentLogged;
	}
	
	public static void logout() throws Exception {
		if(currentLogged == null)
			throw new Exception(ExceptionsTypes.INVALID_TOKEN.name());
		if(DBWrapper.getConnection() == null)
			throw new Exception(ExceptionsTypes.CONNECTION_ERROR.name());
		currentLogged = null;
	}

	public static PermissionList getPermission() throws Exception {
		if(currentLogged == null)
			throw new Exception(ExceptionsTypes.INVALID_TOKEN.name());
		if (currentLogged instanceof Customer) {
			PermissionList list = new PermissionList();
			list.add(Permission.REQUESTS_CREATE);
			list.add(Permission.REQUESTS_VIEW);
			return list;
		}
		else{
			PermissionList list = new PermissionList();
			Employee e = (Employee) currentLogged;
			switch (e.getRole()){
				case RAPRESENTANT:{ 
					list.add(Permission.REQUESTS_CREATE_ALL);
					list.add(Permission.REQUESTS_VIEW_ALL);
					list.add(Permission.REQUESTS_MANAGE);
					list.add(Permission.BUDGETS_VIEW_ALL);
					list.add(Permission.BUDGETS_MANAGE_ALL);
					list.add(Permission.BUDGETS_MODIFY);
					list.add(Permission.CONTRACTS_MANAGE_ALL);
					list.add(Permission.CONTRACTS_MODIFY);
					list.add(Permission.CONTRACTS_VIEW_ALL);
					list.add(Permission.SERVICE_MANAGE);
					list.add(Permission.SERVICE_VIEW);
					break;
				} 
				case EVENT_RESPONSABLE:{
					list.add(Permission.EVENT_VIEW_ALL);
					list.add(Permission.EVENT_CHANGE_STATE);
					list.add(Permission.EVENT_MANAGE_ALL);
					break;
				} 
			case ARTIST:
				break;
			case FINANTIAL_RESPONSABLE:{
				list.add(Permission.TRANSACTION_MANAGE);
				break;
			}
			case HUMAN_RESOURCES_RESPONSABLE:{
				list.add(Permission.EMPLOYEES_VIEW);
				list.add(Permission.EMPLOYEES_MANAGE);
				break;
				}
			case MAITRE:
				break;
			case OTHER_EMPLOYEE:
				break;
			case WAREHOUSE_RESPONSABLE:
				break;
			default:
				break;
			}
			return list;
		}
	}
	
	private static User currentLogged = null;
}
